from playwright.sync_api import sync_playwright, Page
import pandas as pd
from pandas import DataFrame

from common.config import Playwright_DDL_Path, Public_Data
from common.pd import excel_to_db
from project.web_comm import get_token


class PlayWright_Driver:

    def __init__(self):
        self.public_data = Public_Data()
        self.web_page: Page = Page(None)
        self.url = ""

    def set_url(self, url):
        self.url = url

    def add_token_to_url(self, url):
        if not self.public_data.Token:
            self.public_data.Token = get_token()
        if url:
            self.url = url + "&" + self.public_data.Token
            return
        self.url += "&" + self.public_data.Token

    def open_url(self):
        self.web_page.goto(self.url)




def get_excel_ddl_df(excel_path, sheet_name) -> DataFrame:
    """
        id	step_name	method_name	  para	 model_attr

    """
    return pd.read_excel(excel_path, sheet_name)


def start_pw_ddl(model, sheet_name, excel_path=""):
    pw_driver = PlayWright_Driver()

    with sync_playwright() as p:
        if not excel_path:
            excel_path = Playwright_DDL_Path
        browser = p.chromium.launch(headless=False)
        pw_driver.web_page = browser.new_page()
        command_df:DataFrame = get_excel_ddl_df(excel_path, sheet_name)
        row_count,col_count = command_df.shape
        for row_index in range(row_count):
            para_list= []
            step_name =  command_df.loc[row_index,"step_name"]
            command = command_df.loc[row_index,"method_name"]
            command_para :str= command_df.loc[row_index,"para"]
            model_attr = command_df.loc[row_index,"model_attr"]
            if command_para:
                para_list = command_para.split(",")
            if model_attr:
                model_value = get_model_attr(model,model_attr)
                para_list.append(model_value)



def get_model_attr(model,model_attr):
    return  ""
    pass